<?php

namespace App\Admin\Controllers;

use App\Models\UserLevel;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class UserLevelController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('等级');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('等级');
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('等级');
            $content->description('新增');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(UserLevel::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->level_name("等级名称");
            $grid->level_title("职称");
            $grid->fund_ratio("基金(%)");
            $grid->stock_ratio("股权(%)");
            $grid->same_ratio("同阶奖励(%)");
            $grid->auto_update("自动升级")->display(function ($auto_update) {
                if ($auto_update) {
                    return "<span class='label label-primary'>是</span>";
                } else {
                    return "<span class='label label-default'>否</span>";
                }
            });

            $grid->disableExport();
            $grid->disableFilter();
            $grid->disableRowSelector();
            $grid->disableActions();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(UserLevel::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text("level_name", '等级名称');
            $form->text("level_title", '职称');
            $form->number("fund_ratio", '基金比例%')->attribute(["min" => 0]);
            $form->number("stock_ratio", '股权比例%')->attribute(["min" => 0]);
            $form->number("same_ratio", '同阶奖励%')->attribute(["min" => 0]);
            $form->switch('auto_update','自动升级');

//            $form->display('created_at', 'Created At');
//            $form->display('updated_at', 'Updated At');
        });
    }
}
